#include "stdafx.h"

#include "hwindef.h"
#include "hwinstring.h"

namespace harlinn
{
    namespace windows
    {
#ifdef UNICODE
        namespace
        {
            void destroy_array(wchar_t*ptr)
            {
                if(ptr) delete [] ptr;
            }
        }
        
        String HWIN_EXPORT to_string(const char* str)
        {
            if(str == nullptr || str[0] == '\x0')
            {
                return String();
            }
            UINT const cp = GetACP ();  // Codepage
            int const requiredLenght = ::MultiByteToWideChar (cp, 0, str, -1, 0, 0);

            //wchar_t* wstr = new wchar_t[requiredLenght];
            std::unique_ptr<wchar_t[], void (*)(wchar_t*ptr)  > wstr(new wchar_t[requiredLenght],destroy_array);
            ::MultiByteToWideChar (cp, 0, str, -1, wstr.get(), requiredLenght);
            String result = String(wstr.get(),requiredLenght);
            return result;
        }
        String HWIN_EXPORT to_string(const wchar_t* str)
        {
            String result = String(str);
            return result;
        }
#else
        std::shared_ptr<String> HWIN_EXPORT to_string(const char* str)
        {
        }

        std::shared_ptr<String> HWIN_EXPORT to_string(const wchar_t* str)
        {
        }
#endif        


    };
};